package com.aistar.service.impl;

import com.aistar.mapper.ProductReviewImagesMapper;
import com.aistar.mapper.ProductReviewMapper;
import com.aistar.pojo.ProductReview;
import com.aistar.pojo.ProductReviewExample;
import com.aistar.pojo.ProductReviewImages;
import com.aistar.service.UploadImgPicService;
import com.aistar.util.OSSClientUtil;
import com.aistar.util.ServerResponse;
import com.aliyun.oss.OSSClient;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UploadImgPicServiceImpl implements UploadImgPicService {

    private Integer evaluationId = 0;

    @Autowired
    private OSSClient ossClient;

    @Autowired
    private OSSClientUtil aliyunConfig;

    @Autowired
    ProductReviewImagesMapper reviewImagesMapper;

    @Autowired
    ProductReviewMapper productReviewMapper;

    @Override
    public ServerResponse upload(MultipartFile multipartFile,String token) {

        //1、根据token获取对应的评论id
        List<ProductReview> productReviews = new ArrayList<>();
        ProductReviewExample productReviewExample = new ProductReviewExample();
        productReviewExample.createCriteria().andOther1EqualTo(token);
        productReviews = productReviewMapper.selectByExample(productReviewExample);
        if (productReviews.size() == 1) {
            System.out.println("hahhaha...查询评论成功啦");

            //获取评论id
            productReviews.forEach(productReview -> {
                evaluationId = productReview.getId();
                System.out.println("查询到的评论id是。。。" + evaluationId);
            });
        }

        // 2. 准备上传API的参数
        String fileName = multipartFile.getOriginalFilename();
        String filePath = this.getFilePath(fileName);

        // 3. 上传至阿里OSS
        try {
            ossClient.putObject(aliyunConfig.getBucketName(), filePath,
                    new ByteArrayInputStream(multipartFile.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
            return ServerResponse.addFailed();
        }

        //System.out.println("路径：" + aliyunConfig.getUrlPrefix() + filePath);
        String savaUrl = "https://" + aliyunConfig.getUrlPrefix() +"/" + filePath;
        //创建对象往数据库里添加
        ProductReviewImages productReviewImages = new ProductReviewImages();
        productReviewImages.setProductReviewId(evaluationId);
        productReviewImages.setUrl(savaUrl);
        productReviewImages.setStatus("1");
        productReviewImages.setOther1(token);

        reviewImagesMapper.insert(productReviewImages);
        return ServerResponse.addSuccess();
    }

    @Override
    public String getFilePath(String fileName) {
        DateTime dateTime = new DateTime();
        return "images/" + dateTime.toString("yyyy") + "/" + dateTime.toString("MM") + "/"
                + dateTime.toString("dd") + "/" + System.currentTimeMillis() +
                (RandomUtils.nextInt(9999) + 100) + "."
                + org.apache.commons.lang.StringUtils.substringAfterLast(fileName, ".");
    }
}
